<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once ( LIB_DIR.'search/abstractsearch.php');
/**
 * Description of shopsearch
 *
 * @author jszhang
 */
class ReviewSearch extends Search {

    public function ReviewSearch( $host='127.0.0.1', $port=3312 ) {
        parent::__construct($host, $port);

		// reset the hight light option for review
		// we should display all text
		$this->m_highLightOpts = array
		(
			"before_match"          => "<span class=\"hilight\">",
			"after_match"           => "</span>",
			"chunk_separator"       => " ... ",   //A string to insert between snippet chunks
			"limit"                 => 0,
			"around"                => 3,         //How much words to pick around each matching keywords block
			"exact_phrase"          => false,     //Whether to highlight exact query phrase matches only instead of individual keywords
			"single_passage"        => false,     //Whether to extract single best passage only.
		);

        //override some member vars
        $this->m_index = array( "review_index", "review_stemmed" );
    }

    /**
     * override protected functions
     */

    //after some default value changed, invoke this function
    protected function initialize() {
		//clear all filters
		$this->m_sphinxClient->ResetFilters();
		$this->m_sphinxClient->ResetOverrides();
		$this->m_sphinxClient->ResetGroupBy();

        $this->m_sphinxClient->SetConnectTimeout ( 1 );
        $this->m_sphinxClient->SetMatchMode ( $this->m_matchMode );
        $this->m_sphinxClient->SetWeights ( array ( 100, 1 ) );

        $this->m_sphinxClient->SetRankingMode ( $this->m_ranker );
        $this->m_sphinxClient->SetArrayResult ( $this->m_isArrayResult );
        $this->m_perPage = USER_INFO_PER_PAGE;
    }

    //put your code here
    protected function printResult( $res ) {
        $result = array();

        $result['total'] = $res['total'];
        $result['total_found'] = $res['total_found'];
        $result['time'] = $res['time'];

        if ( isset($res["matches"]) && is_array( $res["matches"] ) ) {
            $result['matchIds'] = array();
            foreach ( $res["matches"] as $docinfo ) {
                $result['matchIds'][] = $docinfo['id'];
            }
        }

        return $result;
    }

	// this function does not used in user search
	// just return the empty array
	protected function printGroupByResult( $res, $groupField ){
		$result = array();
		return $result;
	}
	
	/**********************************************************
	 *	Seal Created by 2010-6-19
	 **********************************************************/
	public function GetHotReview( $count )
	{
		
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilterRange( 'review_date', time() - 60*60*24*10,  time() );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_EXTENDED, 'response_count desc, view_count desc, flower_count desc, review_date desc ' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || $re['total'] == 0 )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-7-3
	 **********************************************************/
	public function GetRecentReviewByPlatid( $count, $platId )
	{
		
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'plat_id', array($platId) );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_date' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || $re['total'] == 0 )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-7-31
	 **********************************************************/
	public function GetReviewByTag( $keyword, $page = 1, $uid ) {
		
        $resultArray = array();

        $this->initialize();
        
        $this->m_sphinxClient->SetFilter( 'user_id', array($uid) );

        if ( intval($page) > 0 )
            $this->m_sphinxClient->SetLimits ( ($page-1)*$this->m_perPage, $this->m_perPage );
            
        $resultArray = $this->doSearch( "@tag $keyword" );

        return $resultArray;
    }
}
?>
